package com.best.onegous.details;


import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.best.onegous.BaseActivity;
import com.best.onegous.R;
import com.best.onegous.Adapter.HomeAdapter;
import com.best.onegous.Adapter.PopularAdapter;
import com.best.onegous.Adapter.SearchsAdapter;
import com.best.onegous.Refresh.XListView;
import com.best.onegous.Refresh.XListView.IXListViewListener;
import com.nineoldandroids.view.ViewHelper;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.AbsListView.OnScrollListener;

public class HoneSearch  extends BaseActivity implements OnClickListener,IXListViewListener,OnScrollListener{
	private View search_topview,goodslist_toolbar; 
	private XListView listView;
	private int firstPosition=0;
	private int height;
	private boolean isOpen=true;
	private float count;
	private float scale;
	private HomeAdapter mAdapter;
	private PopupWindow mPopupWindow;
	private LinearLayout.LayoutParams lp;
	private List<JSONObject> mDatas = new ArrayList<JSONObject>();
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
    		switch (msg.what) {
			case 1313://���ض���
				scale=1-(1+((count-height)/height));
				ViewHelper.setTranslationY(search_topview, count-height);
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						ViewHelper.setTranslationY(goodslist_toolbar, count-height);
						 lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);  
		                 
		                 lp.setMargins(0, 0, 0, (int) (count-height)); 
						 goodslist_toolbar.setLayoutParams(lp); 
					}
				}, 40);
				break;
			case 2323://��������
				scale=1-(1+(((height-count)-height)/height));
				ViewHelper.setTranslationY(goodslist_toolbar,(height-count)-height);
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						ViewHelper.setTranslationY(search_topview, (height-count)-height);
						 lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);  
		                 
		                 lp.setMargins(0, 0, 0, (int) (height-count)-height); 
						 goodslist_toolbar.setLayoutParams(lp); 
					}
				}, 40);
				
				break;

			default:
				break;
						
				
			}
    	};
    };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setThem(R.layout.honesearch_activity);
		findViewById(R.id.back).setOnClickListener(this);
		listView = (XListView) findViewById(R.id.listView);
		search_topview = findViewById(R.id.search_topview);
		goodslist_toolbar = findViewById(R.id.goodslist_toolbar);
		
		for (int i = 0; i < 30; i++) {
			mDatas.add(new JSONObject());
		}
		mAdapter = new HomeAdapter(this, mDatas);
		listView.setAdapter(mAdapter);
		listView.setPullRefreshEnable(true);
		listView.setPullLoadEnable(true);
		listView.setXListViewListener(this);
		listView.setOnScrollListener(this);
		
		
		
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			 finish();
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				listView.stopLoadMore();
			}
		}, 2000);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				listView.stopRefresh();
			}
		}, 2000);
	}
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		if(scrollState==OnScrollListener.SCROLL_STATE_IDLE){
			 firstPosition=view.getFirstVisiblePosition();
		}
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		height=search_topview.getHeight();
		if(!isOpen){
			if(firstVisibleItem-firstPosition<=-1){//˵��������-->��ʾ������
				count=height+1;
				new Thread(new Runnable() {
					@Override
					public void run() {
						while (count>0) {
						handler.sendEmptyMessage(2323);
						count--;
						try {
							Thread.sleep(2);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					}
				}).start();
				isOpen=true;
			}
		}
		if(firstVisibleItem-firstPosition>=1){//˵��������-->����������
			if(isOpen){
				count=height+1;
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						while (count>0) {
						handler.sendEmptyMessage(1313);
						count--;
						try {
							Thread.sleep(2);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					}
				}).start();
				isOpen=false;
			}
		}
	}
}
